package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.InsuranceRecordMapper;
import com.vehicle.pojo.InsuranceRecord;
import com.vehicle.service.InsuranceRecordService;
import com.vehicle.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class InsuranceRecordServiceImpl implements InsuranceRecordService {
    @Autowired
    private InsuranceRecordMapper insuranceRecordMapper;

    /**
     * 分页带搜索
     * @param insuranceRecord
     * @return
     */
    @Override
    public DataGridView findInsuranceRecordByPage(InsuranceRecord insuranceRecord) {
        Page page = PageHelper.startPage(insuranceRecord.getPage(),insuranceRecord.getLimit());
        List<InsuranceRecord> insuranceRecords = insuranceRecordMapper.selectInsuranceRecordByPage(insuranceRecord);
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(insuranceRecords);
        return dataGridView;
    }

    @Override
    public DataGridView findAllInsuranceRecord(InsuranceRecord insuranceRecord) {
        DataGridView dataGridView = new DataGridView();
        dataGridView.setData(insuranceRecordMapper.selectInsuranceRecordByPage(new InsuranceRecord()));
        return dataGridView;
    }

    //删除
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteInsuranceRecordByid(int insuranceid) {
        this.insuranceRecordMapper.updateStatusById(insuranceid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBatchInsuranceRecordByIds(Integer[] insuranceIds) {
        for(Integer insuranceId:insuranceIds){
            insuranceRecordMapper.updateStatusById(insuranceId);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addInsuranceRecord(InsuranceRecord insuranceRecord) {

        try {
            insuranceRecord.setInsurance_endtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateUtil.processingTime(insuranceRecord.getInsurance_starttime(),12)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        insuranceRecordMapper.addInsuranceRecord(insuranceRecord);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateInsuranceRecord(InsuranceRecord insuranceRecord) {
        try {
            insuranceRecord.setInsurance_endtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateUtil.processingTime(insuranceRecord.getInsurance_starttime(),12)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        insuranceRecordMapper.updateInsuranceRecord(insuranceRecord);
    }
}
